<template>
	<div class="zy-content" :class="{ tabClass: sourceType === 'tab' }">
		<div class="searchBox search-box-layout">
			<Row :gutter="24">
				<Col span="4">
					<p class="label">设备流水号</p>
					<div class="ipt">
						<Select
							v-if="sourceType !== 'tab'"
							filterable
							v-model="searchParams.deviceId"
							:remote-method="remoteMethod1"
							:loading="loading1"
							clearable
						>
							<Option
								v-for="item of equipmentList"
								:value="item.id"
								:key="item.id"
								:label="item.deviceNo + '-' + item.deviceName + '-' + item.deviceMId"
							></Option>
						</Select>
						<Input disabled v-else :value="params.deviceNo + '-' + params.deviceName + '-' + params.deviceMId" />
					</div>
				</Col>
				<Col span="4">
					<div>
						<p class="label">{{ $t('设备类别') }}</p>
						<div class="ipt">
							<Select
								v-if="sourceType !== 'tab'"
								v-model="searchParams.deviceCategory"
								filterable
								clearable
								:placeholder="`${$t('common.selectInfo')}`"
								@on-change="deviceChange"
								:remote-method="handleRemote"
							>
								<Option v-for="(item, index) of deviceTypeList" :value="item.value" :key="index" :label="item.value" />
							</Select>
							<Input v-else disabled type="text" v-model="searchParams.deviceCategory" />
						</div>
					</div>
				</Col>
				<Col span="4">
					<div>
						<p class="label">{{ $t('规格型号') }}</p>
						<div class="ipt">
							<!-- <Select
                v-if="sourceType !== 'tab'"
                v-model="searchParams.deviceSpec"
                filterable
                clearable
                :placeholder="`${$t('common.selectInfo')}`"
                @on-change="specificationParamsChange"
              >
                <Option
                  v-for="(item, index) of specificationList"
                  :value="item"
                  :key="index"
                  :label="item"
                />
              </Select>
              <Input v-else disabled type="text" v-model="searchParams.deviceSpec" />-->
							<Input :placeholder="`${$t('common.inputInfo')}`" v-model="searchParams.deviceSpec" clearable />
						</div>
					</div>
				</Col>
				<Col span="4">
					<div>
						<p class="label">{{ $t('故障部件') }}</p>
						<div class="ipt">
							<Select v-model="searchParams.troubleUnit" filterable clearable :placeholder="`${$t('common.selectInfo')}`">
								<Option v-for="(item, index) of faultPositionList" :value="item.value" :key="index" :label="item.value" />
							</Select>
						</div>
					</div>
				</Col>
				<Col span="4">
					<div>
						<p class="label">{{ $t('故障分类') }}</p>
						<div class="ipt">
							<Input :placeholder="`${$t('common.inputInfo')}`" v-model="searchParams.faultCategory" clearable />
						</div>
					</div>
				</Col>
				<Col span="4">
					<div>
						<p class="label">{{ $t('故障代码') }}</p>
						<div class="ipt">
							<Input :placeholder="`${$t('common.inputInfo')}`" v-model="searchParams.faultTypeCode" clearable />
						</div>
					</div>
				</Col>
				<Col span="4">
					<div>
						<p class="label">{{ $t('故障描述') }}</p>
						<div class="ipt">
							<Input :placeholder="`${$t('common.inputInfo')}`" v-model="searchParams.faultMessage" clearable />
						</div>
					</div>
				</Col>
				<Col span="4">
					<div>
						<p class="label">{{ $t('故障等级') }}</p>
						<div class="ipt">
							<Select v-model="searchParams.faultGrade" clearable :placeholder="`${$t('common.selectInfo')}`">
								<Option v-for="(item, index) of faultLevel" :value="item" :key="index" :label="item" />
							</Select>
						</div>
					</div>
				</Col>
				<!-- <Col span="4">
          <div>
            <p class="label">{{ $t('是否自动生成维修单') }}</p>
            <div class="ipt">
              <Select
                v-model="searchParams.autogeneration"
                clearable
                :placeholder="`${$t('common.selectInfo')}`"
              >
                <Option
                  v-for="(item, index) of autogenerationList"
                  :value="item.id"
                  :key="index"
                  :label="item.name"
                />
              </Select>
            </div>
          </div>
        </Col> -->
				<Col span="4">
					<div>
						<p class="label hide-label">--</p>
						<div class="ipt">
							<Button class="search-btn" type="primary" @click="changeNum()">{{ $t('common.Query') }}</Button>
							<Button class="reset-btn" @click="reset()">{{ $t('common.reset') }}</Button>
						</div>
					</div>
				</Col>
			</Row>
		</div>

		<div class="list-layout mt16">
			<div class="btns clear">
				<span class="title">{{ $t('common.listInformation') }}</span>
				<Button @click="addClickinfo">{{ $t('common.New') }}</Button>
				<Button @click="exportTemplateClick">
					{{ $t('common.batchExport') }}
				</Button>
				<Button @click="importExcelShow">{{ $t('common.Import') }}</Button>
				<Button @click="exportTemplate">{{ $t('preliminary.DownloadTemplate') }}</Button>
				<Button @click="deleteAll">{{ $t('maintenance.Delete') }}</Button>
				<Button @click="getTableColumns(columns)">{{ handleContextMenuStatus ? $t('显示操作栏') : $t('隐藏操作栏') }}</Button>
			</div>
			<div class="tableContent">
				<Table
					class="resizable-table"
					border
					@on-column-width-resize="onColumnWidthResize"
					:columns="columnsList"
					:data="dataSource"
					@on-selection-change="onSelectChange"
				>
					<template slot-scope="{ row }" slot="action">
						<span class="operationBtn" @click="editRow(row)">
							<i class="iconfont zy-common-bianji"></i>
							{{ $t('common.Edit') }}
						</span>
						<span class="operationBtn" @click="(deleteModal = true), (deleteIds = [row.id])">
							<i class="iconfont zy-common-shanchu"></i>
							{{ $t('maintenance.DeleteObj') }}
						</span>
					</template>
					<template slot-scope="{ row }" slot="file">
						<div>
              <!--  :href="util.filePathToUrl(item.fileUrl)" -->
							<a target="_blank" title="下载"  v-for="(item, index) in row.kbFile" :key="index">
								<Tag color="blue" @click.native="fileDown(item)">
									<i style="color: #b7eb8f !important" class="iconfont zy-common-xiazai"></i>
									{{ item.fileName }}
								</Tag>
								<br />
							</a>
						</div>
					</template>
				</Table>
			</div>
			<div class="pageWrap mt16">
				<pageLeftTitle :total="total" :selected="deleteIds.length" />
				<Page
					:total="total"
					:page-size="pageSize"
					:page-size-opts="pageSizeOption"
					:current="pageNum"
					@on-change="changePageNum"
					@on-page-size-change="changePageSize"
					show-sizer
					show-total
				/>
			</div>
		</div>
		<Modal
			:mask-closable="false"
			v-model="addModal"
			:title="formItem.id ? $t('maintenance.Edit') : $t('maintenance.New')"
			width="800"
			class="modal-content-layout maintainKnowLedgeAdd"
		>
			<Form ref="formItem" class="formList" label-position="top" :model="formItem" :rules="rules">
				<Row :gutter="16">
					<Col :span="12">
						<FormItem label="设备流水号" prop="deviceId">
							<Select
								v-if="sourceType !== 'tab'"
								v-model="formItem.deviceId"
								filterable
								clearable
								:remote-method="remoteMethod1"
								:loading="loading1"
								@on-select="getDev"
							>
								<Option v-for="item of equipmentList" :value="item.id" :key="item.id">{{
									item.deviceNo + '-' + item.deviceName + '-' + item.deviceMId
								}}</Option>
							</Select>
							<Input disabled v-else :value="params.deviceNo + '-' + params.deviceName + '-' + params.deviceMId" />
						</FormItem>
					</Col>
					<Col :span="12">
						<FormItem :label="$t('设备类别')" prop="deviceCategory">
							<Input v-if="sourceType !== 'tab'" v-model="formItem.deviceCategory" disabled />
							<Input v-else v-model="params.deviceCategory" disabled />
						</FormItem>
					</Col>
					<Col :span="12">
						<FormItem :label="$t('规格型号')" prop="deviceSpec">
							<Input v-if="sourceType !== 'tab'" v-model="formItem.deviceSpec" disabled />
							<Input v-else v-model="params.deviceSpec" disabled />
						</FormItem>
					</Col>
					<Col :span="12">
						<FormItem :label="$t('故障部件')" prop="troubleUnit">
							<Select v-model="formItem.troubleUnit" filterable clearable :placeholder="`${$t('common.selectInfo')}`">
								<Option v-for="(item, index) of faultPosition" :value="item.value" :key="index" :label="item.value" />
							</Select>
						</FormItem>
					</Col>
					<Col :span="12">
						<FormItem :label="$t('fault.FaultClassification')" prop="faultCategory">
							<Input :placeholder="`${$t('common.inputInfo')}`" v-model="formItem.faultCategory" clearable />
						</FormItem>
					</Col>
					<Col :span="12">
						<FormItem :label="$t('fault.RepairingMode')" prop="maintenanceMode">
							<Select v-model="formItem.maintenanceMode" filterable clearable :placeholder="`${$t('common.selectInfo')}`">
								<Option v-for="(item, index) of maintainTypeList" :value="item.label" :key="index" :label="item.label" />
							</Select>
						</FormItem>
					</Col>
					<Col :span="12">
						<FormItem :label="$t('fault.CauseAnalysis')" prop="analysisOfCauses">
							<Input :placeholder="`${$t('common.inputInfo')}`" v-model="formItem.analysisOfCauses" clearable />
						</FormItem>
					</Col>
					<Col :span="12">
						<FormItem :label="$t('fault.solution')" prop="solution">
							<Input :placeholder="`${$t('common.inputInfo')}`" v-model="formItem.solution" clearable />
						</FormItem>
					</Col>
					<Col :span="12">
						<FormItem :label="$t('故障代码')" prop="faultTypeCode">
							<Input :placeholder="`${$t('common.inputInfo')}`" v-model="formItem.faultTypeCode" clearable />
						</FormItem>
					</Col>
					<Col :span="12">
						<FormItem :label="$t('故障描述')" prop="faultMessage">
							<Input :placeholder="`${$t('common.inputInfo')}`" v-model="formItem.faultMessage" clearable />
						</FormItem>
					</Col>
					<Col :span="12">
						<FormItem :label="$t('故障等级')" prop="faultGrade">
							<Select v-model="formItem.faultGrade" clearable :placeholder="`${$t('common.selectInfo')}`">
								<Option v-for="(item, index) of faultLevel" :value="item" :key="index" :label="item" />
							</Select>
						</FormItem>
					</Col>
					<!-- <Col :span="12">
            <FormItem :label="$t('是否自动生成维修单')" prop="autogeneration">
              <Select
                v-model="formItem.autogeneration"
                clearable
                :placeholder="`${$t('common.selectInfo')}`"
              >
                <Option
                  v-for="(item, index) of autogenerationList"
                  :value="item.id"
                  :key="index"
                  :label="item.name"
                />
              </Select>
            </FormItem>
          </Col> -->
					<Col :span="24">
						<FormItem :label="$t('fault.sop')" prop="kbFile">
							<Upload
								ref="upload"
								:show-upload-list="true"
								:default-file-list="formItem.kbFile"
								:on-success="handleSuccess"
								:on-remove="onRmove"
								type="drag"
								:action="fileUploadUrl"
								:data="{ dir: 'device' }"
							>
								<Button type="primary">{{ $t('common.Upload') }}</Button>
							</Upload>
						</FormItem>
					</Col>
				</Row>
			</Form>
			<div slot="footer">
				<Button type="default" @click="addModal = false">{{ $t('common.Cancle') }}</Button>
				<Button type="primary" @click="handleSubmit('formItem')">{{ $t('maintenance.Confirmed') }}</Button>
			</div>
		</Modal>

		<Modal :mask-closable="false" v-model="deleteModal" :title="$t('common.Delete')" class="modal-title-layout">
			<div class="icon red">
				<i class="iconfont zy-common-shanchu"></i>
			</div>
			<p class="title">
				<span>{{ $t('common.confirmDelete') }}</span>
			</p>
			<div slot="footer">
				<Button type="default" @click="deleteModal = false">{{ $t('common.Cancle') }}</Button>
				<Button type="primary" @click="handleDelete">{{ $t('maintenance.Confirmed') }}</Button>
			</div>
		</Modal>
		<importExcelModal
			:importExcelStateF="importExcelState"
			v-if="importExcelState"
			url="/dms/devMaintainKb/importDevMaintainKb"
			@importExcelChange="importExcelChange"
		></importExcelModal>
	</div>
</template>

<script>
import fileDownload from 'js-file-download';
import importExcelModal from '@/components/importExcelNew.vue';
import util from '@/util/util';
import toggleTableOperation from '@/mixins/toggleTableOperation';
export default {
	name: 'faultClassify',
	mixins: [toggleTableOperation],
	components: { importExcelModal },
	props: ['sourceType', 'params'],
	data() {
		const validatorTrouble = (rule, value, callback) => {
			if (!value && value != 0) {
				callback(new Error(this.$t('common.selectInfo')));
			} else {
				callback();
			}
		};
		return {
			autogenerationList: [
				{ id: 1, name: this.$t('是') },
				{ id: 0, name: this.$t('否') },
			],
			deviceTypeList: [],
			total: 0, // 总数
			pageNum: 1, // 页数
			pageSize: 10, // 每页的个数
			pageSizeOption: [10, 20, 30],
			addModal: false, // 新增弹窗
			// addtitle: "新增故障分类", // 新增/编辑 标题
			formItem: {
				kbFile: [],
			},
			rules: {
				// 校验规则
				deviceId: [
					{
						required: true,
						type: 'number',
						message: this.$t('common.selectInfo'),
						trigger: 'change',
					},
				],
				deviceNo: [
					{
						required: true,
						message: this.$t('common.selectInfo'),
						trigger: 'change',
					},
				],
				deviceCategory: [{ required: true, trigger: 'change', validator: validatorTrouble }],
				troubleUnit: [{ required: true, trigger: 'blur', validator: validatorTrouble }],
				faultCategory: [
					{
						required: true,
						message: this.$t('common.selectInfo'),
						trigger: 'blur',
					},
				],
				maintenanceMode: [{ required: true, trigger: 'change', validator: validatorTrouble }],
				analysisOfCauses: [
					{
						required: true,
						message: this.$t('common.PleaseEenter'),
						trigger: 'change',
					},
				],
				solution: [
					{
						required: true,
						message: this.$t('common.PleaseEenter'),
						trigger: 'change',
					},
				],
				deviceSpec: [{ required: true, trigger: 'change', validator: validatorTrouble }],
			},
			searchParams: {
				troubleUnit: '',
			},
			upgradeNodeList: [{ id: 1, name: '123' }],
			deleteModal: false, // 删除弹窗
			deleteIds: [], // 删除Id
			importExcelState: false, // 导入弹窗
			columns: [
				{
					key: 'selection',
					type: 'selection',
					width: 60,
					align: 'center',
				},
				{
					title: `${this.$t('设备流水号')}`,
					key: 'deviceMId',
					align: 'center',
					minWidth: 180,
					tooltip: true,
					resizable: true,
				},
				{
					title: `${this.$t('设备编号')}`,
					key: 'deviceNo',
					align: 'center',
					minWidth: 180,
					tooltip: true,
					resizable: true,
				},
				{
					title: `${this.$t('设备名称')}`,
					key: 'deviceName',
					align: 'center',
					minWidth: 180,
					tooltip: true,
					resizable: true,
				},
				{
					title: `${this.$t('设备类别')}`,
					key: 'deviceCategory',
					align: 'center',
					minWidth: 180,
					tooltip: true,
					resizable: true,
				},
				{
					title: `${this.$t('规格型号')}`,
					key: 'deviceSpec',
					align: 'center',
					minWidth: 100,
					tooltip: true,
					resizable: true,
				},
				{
					title: `${this.$t('故障部件')}`,
					key: 'troubleUnit',
					align: 'center',
					minWidth: 150,
					tooltip: true,
					resizable: true,
				},
				{
					title: `${this.$t('故障代码')}`,
					key: 'faultTypeCode',
					align: 'center',
					minWidth: 180,
					tooltip: true,
					resizable: true,
				},
				{
					title: `${this.$t('故障描述')}`,
					key: 'faultMessage',
					align: 'center',
					minWidth: 180,
					tooltip: true,
					resizable: true,
				},
				{
					title: `${this.$t('故障等级')}`,
					key: 'faultGrade',
					align: 'center',
					minWidth: 180,
					tooltip: true,
					resizable: true,
				},
				{
					title: `${this.$t('fault.FaultClassification')}`,
					key: 'faultCategory',
					align: 'center',
					minWidth: 150,
					tooltip: true,
					resizable: true,
				},
				{
					title: `${this.$t('fault.RepairingMode')}`,
					key: 'maintenanceMode',
					align: 'center',
					minWidth: 150,
					tooltip: true,
					resizable: true,
				},
				{
					title: `${this.$t('fault.CauseAnalysis')}`,
					key: 'analysisOfCauses',
					align: 'center',
					minWidth: 150,
					tooltip: true,
					resizable: true,
				},
				{
					title: `${this.$t('fault.solution')}`,
					key: 'solution',
					align: 'center',
					minWidth: 150,
				},
				{
					title: `${this.$t('fault.sop')}`,
					key: 'deviceName',
					align: 'center',
					slot: 'file',
					minWidth: 200,
					tooltip: true,
					resizable: true,
				},
				// {
				//   title: `${this.$t("是否自动生成维修单")}`,
				//   key: "autogeneration",
				//   align: "center",
				//   minWidth: 180,
				//   render: (h, params) => {
				//     return h('div', params.row.autogeneration==1 ? '是':'否')
				//   }
				// },
				{
					title: `${this.$t('创建人')}`,
					key: 'realname',
					align: 'center',
					minWidth: 200,
					tooltip: true,
					resizable: true,
				},
				{
					title: `${this.$t('创建时间')}`,
					key: 'createTime',
					align: 'center',
					minWidth: 180,
					tooltip: true,
					resizable: true,
				},
				{
					title: `${this.$t('common.operation')}`,
					slot: 'action',
					align: 'center',
					fixed: 'right',
					width: 180,
				},
			],
			dataSource: [],
			equipmentType: [
				// 设备类别
				{ id: 'A', name: 'A类' },
				{ id: 'B', name: 'B类' },
				{ id: 'C', name: 'C类' },
			],
			faultCategoryList: [], // 故障分类
			deviceList: [],
			deviceList2: [],
			faultPosition: [],
			maintainTypeList: [
				{ value: 0, label: '自行维修' },
				{ value: 1, label: '保修期内维修' },
				{
					value: 2,
					label: '委外维修',
				},
			],
			files: [],
			formFiles: [],
			fileUploadUrl: '',
			deviceSpec: [],
			specificationList: [],
			faultPositionList: [],
			equipmentList: [],
			loading1: false,
			faultLevel: ['A', 'B', 'C'],
		};
	},
	created() {
		this.getTableColumnsWidth(this.columns);

		this.fileUploadUrl = `${util.getEnv()}/fileServer/upload`;

		this.getFaultPosition(this.searchParams, 1);
		if (this.sourceType === 'tab') {
			this.searchParams = Object.assign(this.searchParams, {
				...this.params,
				deviceId: this.params.id,
			});
			this.getFaultPosition(this.searchParams, 2);
			this.getFaultPosition(this.searchParams, 1);

			// this.equipmentList = [{deviceNo, deviceName, deviceMId,deviceCategory, deviceSpec}];
		}

		this.getFaultCategory('故障分类');
		this.getFaultCategory('设备类别');
		this.searchList();
		// this.getDeviceList()
	},
	methods: {
		getEquipmentValue(device, deviceId) {
			this.axios({
				method: 'post',
				url: '/dms/device/page',
				data: {
					deviceMId: device,
					pageSize: 99,
					pageNum: 1,
					// hierarchyCode: this.paramsList.hierarchyCode
				},
			}).then((res) => {
				if (res.code == 200) {
					this.equipmentList = res.data.records;
					if (deviceId) {
						this.formItem.deviceId = deviceId;
					}
				}
			});
		},
		getDev(query) {
			// deviceSpec  deviceCategory-----------------------------------
			const obj = this.equipmentList.find((item) => item.id === query.value);
			this.formItem.deviceCategory = obj.deviceCategory;
			this.formItem.deviceSpec = obj.deviceSpec;
			// this.getFaultPosition({
			//   deviceMId: obj.deviceMId
			// });
		},
		remoteMethod1(query) {
			if (query !== '') {
				this.loading1 = true;
				setTimeout(() => {
					this.loading1 = false;
					this.getEquipmentValue(query); //以输入的设备名去查询
				}, 200);
			} else {
				this.equipmentList = [];
			}
		},
		addModalMethod() {
			this.addModal = true;
			this.$refs['formItem'].resetFields();
			setTimeout(() => {
				if (this.sourceType === 'tab') {
					this.formItem.deviceSpec = this.params.deviceSpec;
					this.formItem.deviceCategory = this.params.deviceCategory;
				}
			}, 200);
		},
		handleRemote(val) {
			// this.getDeviceList(val,true)
		},
		deviceChange(val) {
			if (val) {
				this.searchParams.deviceSpec = '';
				this.specificationList = [];
				this.getSpecification(this.searchParams.deviceCategory, 1);
				if (this.searchParams.deviceSpec)
					this.getFaultPosition(
						{
							deviceCategory: this.searchParams.deviceCategory,
							deviceSpec: this.searchParams.deviceSpec,
							param: '',
						},
						1
					);
				this.faultPositionList = [];
				this.searchParams.troubleUnit = '';
			} else {
				this.searchParams.deviceSpec = '';
				this.specificationList = [];
				this.faultPositionList = [];
				this.searchParams.troubleUnit = '';
			}
		},
		specificationParamsChange(val) {
			if (val) {
				if (this.searchParams.deviceCategory) {
					this.getFaultPosition(
						{
							deviceCategory: this.searchParams.deviceCategory,
							deviceSpec: this.searchParams.deviceSpec,
							param: '',
						},
						1
					);
					this.faultPositionList = [];
					this.searchParams.troubleUnit = '';
				}
			} else {
				this.faultPositionList = [];
				this.searchParams.troubleUnit = '';
			}
		},
		addClickinfo() {
			this.formItem = {
				kbFile: [],
			};
			this.$refs['formItem'].resetFields();

			if (this.sourceType === 'tab') {
				const { deviceNo, deviceName, deviceMId, deviceSpec, deviceCategory, id } = this.params;

				this.formItem.deviceSpec = deviceSpec;
				this.formItem.deviceCategory = deviceCategory;
				this.formItem.deviceNo = deviceNo;
				this.formItem.deviceName = deviceName;
				this.formItem.deviceMId = deviceMId;
				this.formItem.deviceId = id;
			}

			this.addModal = true;
		},
		editRow(row) {
			this.$refs['formItem'].resetFields();
			this.files = [];
			this.formFiles = [];
			this.getEquipmentValue(row.deviceMId, row.deviceId);
			const {
				deviceId,
				faultType,
				troubleUnit,
				faultCategory,
				maintenanceMode,
				analysisOfCauses,
				solution,
				deviceMId,
				id,
				deviceCategory,
				kbFile,
				deviceSpec,
				materielName,
				autogeneration,
				faultMessage,
				faultTypeCode,
				faultGrade,
			} = row;
			this.formItem = {
				deviceId,
				faultType,
				troubleUnit,
				faultCategory,
				maintenanceMode,
				analysisOfCauses,
				solution,
				deviceNo: deviceMId,
				deviceCategory,
				id,
				kbFile,
				deviceSpec,
				autogeneration,
				faultMessage,
				faultTypeCode,
				faultGrade,
			};
			this.addModal = true;

			kbFile.forEach((item) => {
				item.createBy = localStorage.getItem('ziyunFactoryUserId');
			});

			this.formFiles = kbFile;

			this.faultPosition = [
				{
					materielName,
					id: troubleUnit,
				},
			];

			this.files = kbFile.map((item) => {
				return {
					url: item.fileUrl,
					name: item.fileName,
				};
			});

			this.getSpecification();

			this.getFaultPosition(
				{
					deviceMId,
				},
				2,
				troubleUnit
			);
		},
		onRmove(file, fileList) {
			this.files = fileList;
			const obj = this.formFiles.find((item) => file.url === item.filePath);
			if (obj) {
				this.formFiles.splice(this.formFiles.indexOf(obj), 1);
			}
		},
		handleSuccess(res) {
			if (res.code === 200) {
				this.formItem.kbFile.push({...res.data,filePath:res.data.filepath});
			}
		},
		specificationChange(val) {
			if (val) {
				if (this.formItem.deviceCategory) {
					this.faultPosition = [];
					this.formItem.troubleUnit = '';
				}
			} else {
				this.faultPosition = [];
				this.formItem.troubleUnit = '';
			}
		},
		//获取规格
		getSpecification(deviceCategory, type) {
			this.axios({
				method: 'get',
				url: '/dms/dmsDeviceTz/getSpecification',
				params: {
					deviceCategory: deviceCategory || this.formItem.deviceCategory,
				},
			}).then((res) => {
				if (res.code === 200) {
					if (type == 1) {
						this.specificationList = res.data;
					} else {
						this.deviceSpec = res.data;
					}
				} else {
					// this.$Message.warning({
					//   content: res.msg,
					//   duration: 6,
					//   closable: true
					// });
				}
			});
		},

		//获取故障数据
		getFaultPosition(params, type) {
			this.axios({
				method: 'get',
				url: '/dms/faultCategory/dictList?name=部件',
				data: params || {
					deviceCategory: this.formItem.deviceCategory,
					deviceName: this.formItem.deviceName,
					param: '',
				},
			}).then((res) => {
				if (res.code === 200) {
					this.faultPosition = res.data;
					if (type == 1) {
						this.faultPositionList = res.data;
					} else {
						this.faultPosition = res.data;
					}
				} else {
					this.$Message.warning({
						content: res.msg,
						duration: 6,
						closable: true,
					});
				}
			});
		},
		//获取故障数据
		getFaultCategory(name) {
			this.axios({
				method: 'get',
				url: '/dms/faultCategory/dictList',
				params: { name },
			}).then((res) => {
				if (res.code === 200) {
					/*if (name === '故障分类') {
            this.faultCategoryList = res.data.map(item => {
              if (item.value) return item;
            })
          } else*/ if (name === '设备类别') {
						this.deviceTypeList = res.data.map((item) => {
							if (item.value) return item;
						});
					}
				} else {
					this.$Message.warning({
						content: res.msg,
						duration: 6,
						closable: true,
					});
				}
			});
		},

		// 新增提交
		handleSubmit(form) {
			this.$refs[form].validate((valid) => {
				if (valid) {
					const { id } = this.formItem;
					const uid = window.localStorage.getItem('ziyunFactoryUserId');

					let params = {
						...this.formItem,
						createBy: uid,
					};

					if (this.sourceType === 'tab') {
						const { id } = this.params;
						params.deviceId = id;
					}

					this.axios({
						method: 'post',
						url: id ? '/dms/devMaintainKb/updateDevMaintainKb' : '/dms/devMaintainKb/addDevMaintainKb',
						data: params,
					}).then((res) => {
						if (res.code === 200) {
							this.addModal = false;
							this.$Message.success('新增成功');
							this.changeNum();
						} else {
							this.$Message.warning({
								content: res.msg,
								duration: 6,
								closable: true,
							});
						}
					});
				}
			});
		},
		// 删除操作
		handleDelete() {
			this.axios({
				method: 'post',
				url: '/dms/devMaintainKb/delByIds',
				data: this.deleteIds,
			}).then((res) => {
				if (res.code === 200) {
					this.deleteModal = false;
					this.$Message.success('删除成功');
					this.changeNum();
					this.deleteIds = [];
				} else {
					this.$Message.warning({
						content: res.msg,
						duration: 6,
						closable: true,
					});
				}
			});
		},
		reset() {
			if (this.sourceType !== 'tab') {
				this.searchParams = {};
			} else {
				if (this.sourceType === 'tab') {
					const { deviceNo, deviceName, deviceMId, deviceSpec, deviceCategory, id } = this.params;
					this.searchParams = Object.assign(
						{},
						{
							deviceSpec,
							deviceCategory,
							deviceNo,
							deviceName,
							deviceMId,
							deviceId: id,
						}
					);

					this.$forceUpdate();
				}
			}

			this.changeNum();
		},
		//分页
		changeNum() {
			this.pageNum = 1;
			this.searchList();
		},
		changePageSize(pageSize) {
			this.pageSize = pageSize;
			this.searchList();
		},
		changePageNum(val) {
			this.pageNum = val;
			this.searchList();
		},
		//查询列表
		searchList() {
			let params = {
				...this.searchParams,
				pageNum: this.pageNum,
				pageSize: this.pageSize,
			};
			this.axios({
				method: 'post',
				// url: '/dms/knowledgeBase/list',
				// url: "/dms/devMaintainKb/getDevMaintainKbList",
				url: '/dms/devMaintainKb/getDevMaintainKbList',
				data: params,
			}).then((res) => {
				if (res.code === 200) {
					this.dataSource = res.data.records.map((item) => {
						return {
							...item,
							kbFile: item.kbFile || [],
						};
					});
					this.total = res.data.total;
				} else {
					this.$Message.warning({
						content: res.msg,
						duration: 6,
						closable: true,
					});
				}
			});
		},
		onSelectChange(selection) {
			this.deleteIds = selection.map((item) => item.id);
		},
		//下载模板
		exportTemplate() {
			const filName = '维修知识库下载模板';
			this.axios({
				method: 'post',
				url: '/dms/devMaintainKb/exportDevMaintainKbTemplate',
				responseType: 'blob',
			}).then((res) => {
				fileDownload(res, filName + '.xlsx');
			});
		},
		//导出
		exportTemplateClick() {
			let params = {
				categoryName: this.searchParams.categoryName,
				deviceName: this.searchParams.deviceName,
				faultCategoryId: this.searchParams.faultCategoryId,
				faultPart: this.searchParams.faultPart,
				ids: this.deleteIds,
			};
			const filName = '维修知识库';
			this.axios({
				method: 'post',
				url: '/dms/devMaintainKb/exportDevMaintainKb',
				responseType: 'blob',
				data: params,
			}).then((res) => {
				fileDownload(res, filName + '.xlsx');
			});
		},
    fileDown(fileInfo){
      console.log(util.filePathToUrl(fileInfo.filePath),fileInfo.filePath);
      this.axios({
				method: 'get',
				url:util.filePathToUrl(fileInfo.filePath),
				responseType: 'blob',
			}).then((res) => {
				fileDownload(res, fileInfo.fileName);
			});
    },
		/*导入*/
		importExcelShow() {
			this.importExcelState = true;
		},
		importExcelChange(val) {
			this.importExcelState = false;
			if (val === 'success') {
				this.$Message.success('导入成功');
				this.changeNum();
			}
		},
		// 批量删除
		deleteAll() {
			if (this.deleteIds.length == 0) {
				this.$Message.warning({
					content: `${this.$t('common.deleteItems')}`,
					duration: 6,
					closable: true,
				});
				return;
			} else {
				this.deleteModal = true;
			}
		},
	},
};
</script>

<style lang="scss">
.maintainKnowLedgeAdd {
	.ivu-upload {
		display: inline-block;

		.ivu-upload-drag {
			border: none;
		}
	}
}

.tabClass {
	margin-left: 0 !important;
}
</style>
